package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     * */
    @GetMapping
    public ResponseEntity usrs(Long userID, @RequestHeader("Authorization")String token){
        if (userID == null){
            Claims claims = JwtUtils.getClaims(token);
            Integer id = (Integer)claims.get("id");
            userID = Long.valueOf(id);
        }

        UserInfoVo userInfoVo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户资料
     * */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo
                                         /*@RequestHeader("Authorization") String token*/){
      /*  //判断token是否合法
        boolean b = JwtUtils.verifyToken(token);
        if (!b){
            return ResponseEntity.status(401).body(null);
        }*/

     /*   //解析token 向userInfo 中设置 用户 id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer)claims.get("id");
        userInfo.setId(Long.valueOf(id));
*/
        //调用service 更新userInfo 信息
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);

    }

}
